package com.xx.news.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.xx.news.R;
import com.xx.news.entity.Shipin;

import java.util.List;

public class ShipinAdapter extends RecyclerView.Adapter<ShipinAdapter.ViewHolder> {
    private List<Shipin.ShipinBean> mList;
    private OnItemClickListener mListener;
    Context context;

    public ShipinAdapter(List<Shipin.ShipinBean> list, Context context) {
        this.context = context;
        mList = list;
    }

    public void updateData(List<Shipin.ShipinBean> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ShipinAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shipin_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ShipinAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Shipin.ShipinBean resultDTO = mList.get(position);
        Glide.with(holder.image.getContext())
                .asBitmap()
                .load(resultDTO.getCover())
                .into(holder.image);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(resultDTO);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
        }
    }

    public void addItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(Shipin.ShipinBean item);
    }
}
